<?php $this->Html->addCrumb('Trang chủ','/')?>
<?php $this->Html->addCrumb('Kết quả tìm kiếm')?>

<div class="leftColumn">
<?php echo $this->Form->create('null',array('type'=>'get','id'=>'searchForm'))?>
<ul>
	<li>Tìm kiếm nâng cao</li>
	<li><?php echo $this->Form->input('field',array('label'=>'Từ khoá','value'=>$field,'id'=>'fieldSearch'))?></li>
	<li>Loại sản phẩm:<br/>
	<select name='category_id'>
		<option value=''>Chọn loại sản phẩm</option>
		<?php foreach($categories as $item):?>
		<option value='<?php echo $item['Category']['id']?>' <?php if($category_id == $item['Category']['id']) echo 'selected'?>><?php echo $item['Category']['name']?></option>
		<?php endforeach;?>
	</select>
	</li>
	<li>Nhà sản xuất:<br/>
	<select name='brand_id'>
		<option value=''>Chọn nhà sản xuất</option>
		<?php foreach($brands as $item):?>
		<option value='<?php echo $item['Brand']['id']?>' <?php if($brand_id == $item['Brand']['id']) echo 'selected'?>><?php echo $item['Brand']['name']?></option>
		<?php endforeach;?>
	</select>
	</li>
	<li>Xuất xứ:<br/>		
		<select name='source'>
			<option value=''>Chọn xuất xứ</option>
		<?php foreach($sources as $item):?>
			<option value='<?php echo $item['Product']['source']?>' <?php if($source == $item['Product']['source']) echo 'selected'?>><?php echo $item['Product']['source']?></option>
		<?php endforeach;?>
		</select>
	</li>
	<li> 
	<?php echo $this->Form->input('price_from',array('label'=>'Giá từ','value'=>$price_from,'div'=>false,'size'=>10,'class'=>'number'))?> VND
	<?php echo $this->Form->input('price_to',array('label'=>'đến','value'=>$price_to,'div'=>false,'size'=>10,'class'=>'number'))?> VND
	</li>
	<li><?php echo $this->Form->end('Tìm kiếm')?></li>
</ul>
<?php echo $this->Form->end()?>
</div>

<div class="mainColumn">
<h3>Kết quả tìm kiếm theo từ khoá: <?php echo $field?></h3>Tìm được <?php echo count($rs)?> sản phẩm
	<?php
		if($rs):
			$end = count($rs) - 1;
	?>
	<div class="productList">
		<?php
		$i = 0;
		foreach($rs as $key=>$item):
		$i++;
		?>
		<div class="productListItem<?php if($i % 4 == 0) echo ' productListItemEnd'?>">
			<?php
			$url = '/san-pham/'.$item['Product']['slug'];
			$image_path = '/upload_files/'.$item['Product']['photo_dir'].'/'.$item['Product']['photo'];
			if(!file_exists(WWW_ROOT.$image_path)){
				$image_path = 'no_image.jpg';
			}
			echo $this->Html->image($image_path,array('alt'=>$item['Product']['name'],'title'=>$item['Product']['name'],'url'=>$url,'width'=>140));
			?>
			<div><strong><?php echo $item['Product']['name']?></strong></div>
			<strong>Mã SP:</strong> <?php echo $item['Product']['product_code']?><br>
			<strong>Giá:</strong> <span class="productListItemPrice"><?php echo number_format($item['Product']['price'])?> VND</span>
		</div>
		<?php endforeach;?>					
	</div>
	<?php else:?>
	Xin lỗi hiện giờ chưa có sản phẩm nào thuộc mục này
	<?php endif;?>
	<?php echo $this->element('paging')?>
</div>
<script language='javascript' type='text/javascript'>
$().ready(function(){
	$('#searchForm').submit(function(e){
		e.preventDefault();
		var field = $('input[id="fieldSearch"]').val();
		var category_id = $('select[name="category_id"]').val();
		var brand_id = $('select[name="brand_id"]').val();
		var source = $('select[name="source"]').val();
		var price_from = $('input[name="price_from"]').val();
		var price_to = $('input[name="price_to"]').val();		
		$.ajax({
			url : '<?php echo $this->Html->url(array('action'=>'search','controller'=>'products'))?>',
			data : {
				field : field,
				category_id : category_id,				
				brand_id : brand_id,				
				source : source,				
				price_from : price_from,				
				price_to : price_to,				
			},
			//dataType : 'json',
			success : function(html, textStatus) {
				$('.mainColumn').html(html);
				//alert('Success ' + textStatus + html);
			},
			error : function(xhr, textStatus, errorThrown) {
				//alert('An error occurred! ' + errorThrown);
			}
		});
	});
});
</script>